package com.bank.ui.admin;

import com.bank.dao.DepositTypeDAO;
import com.bank.entity.DepositType;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class QueryDepositTypeDialog extends JDialog {
    private List<DepositType> searchResult;

    public QueryDepositTypeDialog() {
        setTitle("存款业务查询");
        setSize(450, 350);
        setLocationRelativeTo(null);
        setModal(true);

        // 使用GridBagLayout以获得更灵活的布局
        JPanel panel = new JPanel(new GridBagLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;

        // === 1. 精确业务编号查询（独立区域）===
        gbc.gridy = 0;
        gbc.gridx = 0;
        gbc.gridwidth = 2;
        panel.add(new JLabel("按业务编号精确查询:"), gbc);

        gbc.gridy = 1;
        gbc.gridx = 0;
        gbc.gridwidth = 1;
        panel.add(new JLabel("业务编号:"), gbc);

        gbc.gridx = 1;
        JTextField exactIdField = new JTextField();
        panel.add(exactIdField, gbc);

        // 分隔线
        gbc.gridy = 2;
        gbc.gridx = 0;
        gbc.gridwidth = 2;
        panel.add(new JSeparator(JSeparator.HORIZONTAL), gbc);

        // === 2. 业务名称模糊查询（独立区域）===
        gbc.gridy = 3;
        gbc.gridx = 0;
        gbc.gridwidth = 2;
        panel.add(new JLabel("按业务名称模糊查询:"), gbc);

        gbc.gridy = 4;
        gbc.gridx = 0;
        gbc.gridwidth = 1;
        panel.add(new JLabel("业务名称:"), gbc);

        gbc.gridx = 1;
        JTextField nameField = new JTextField();
        panel.add(nameField, gbc);

        // 分隔线
        gbc.gridy = 5;
        gbc.gridx = 0;
        gbc.gridwidth = 2;
        panel.add(new JSeparator(JSeparator.HORIZONTAL), gbc);

        // === 3. ID范围查询（独立区域）===
        gbc.gridy = 6;
        gbc.gridx = 0;
        gbc.gridwidth = 2;
        panel.add(new JLabel("按ID范围查询:"), gbc);

        gbc.gridy = 7;
        gbc.gridx = 0;
        gbc.gridwidth = 1;
        panel.add(new JLabel("ID范围:"), gbc);

        gbc.gridx = 1;
        JPanel idRangePanel = new JPanel(new GridLayout(1, 4, 5, 5));
        idRangePanel.add(new JLabel("从:"));
        JTextField startIdField = new JTextField(5);
        idRangePanel.add(startIdField);
        idRangePanel.add(new JLabel("到:"));
        JTextField endIdField = new JTextField(5);
        idRangePanel.add(endIdField);
        panel.add(idRangePanel, gbc);

        // === 按钮区域 ===
        gbc.gridy = 8;
        gbc.gridx = 0;
        gbc.gridwidth = 2;
        gbc.fill = GridBagConstraints.NONE;
        gbc.anchor = GridBagConstraints.CENTER;

        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));
        JButton searchButton = new JButton("查询");
        JButton cancelButton = new JButton("取消");
        searchButton.setPreferredSize(new Dimension(100, 30));
        cancelButton.setPreferredSize(new Dimension(100, 30));
        buttonPanel.add(searchButton);
        buttonPanel.add(cancelButton);
        panel.add(buttonPanel, gbc);


// 在 searchButton 的 ActionListener 中修改逻辑
        searchButton.addActionListener(e -> {
            String exactIdStr = exactIdField.getText().trim();
            String name = nameField.getText().trim();
            String startIdStr = startIdField.getText().trim();
            String endIdStr = endIdField.getText().trim();

            try {
                DepositTypeDAO dao = new DepositTypeDAO();
                System.out.println("【调试】开始存款业务查询...");

                // === 修复1: 精确查询独立处理 ===
                if (!exactIdStr.isEmpty()) {
                    try {
                        int exactId = Integer.parseInt(exactIdStr);
                        System.out.println("【调试】尝试精确查询 ID=" + exactId);

                        // 修复2: 使用正确的方法名
                        DepositType depositType = dao.getDepositTypeByID(exactId);

                        if (depositType != null) {
                            System.out.println("【调试】找到存款类型: ID=" + depositType.getSavingID() +
                                    ", 名称=" + depositType.getSavingName());
                            searchResult = Arrays.asList(depositType);
                            dispose();  // 关闭对话框
                            return;     // 直接返回
                        } else {
                            System.out.println("【调试】未找到业务编号为 " + exactId + " 的存款业务");

                            // 修复3: 清除其他查询条件，只保留精确查询结果
                            nameField.setText("");
                            startIdField.setText("");
                            endIdField.setText("");

                            JOptionPane.showMessageDialog(this,
                                    "未找到业务编号为 " + exactId + " 的存款业务",
                                    "查询结果",
                                    JOptionPane.INFORMATION_MESSAGE);
                            dispose(); // 关闭对话框
                            return;
                        }
                    } catch (NumberFormatException ex) {
                        System.out.println("【调试】业务编号格式错误: " + exactIdStr);
                        JOptionPane.showMessageDialog(this,
                                "业务编号必须为整数",
                                "输入错误",
                                JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                }

                // 2. 名称模糊查询
                if (!name.isEmpty()) {
                    System.out.println("【调试】尝试名称模糊查询: " + name);
                    searchResult = dao.getDepositTypesByNameLike(name);
                    System.out.println("【调试】名称模糊查询结果: " + searchResult.size() + " 条记录");
                }
                // 3. ID范围查询
                else if (!startIdStr.isEmpty() && !endIdStr.isEmpty()) {
                    try {
                        int startId = Integer.parseInt(startIdStr);
                        int endId = Integer.parseInt(endIdStr);
                        System.out.println("【调试】尝试ID范围查询: " + startId + " - " + endId);
                        searchResult = dao.getDepositTypesByIDRange(startId, endId);
                        System.out.println("【调试】ID范围查询结果: " + searchResult.size() + " 条记录");
                    } catch (NumberFormatException ex) {
                        System.out.println("【调试】ID范围格式错误: " + startIdStr + " - " + endIdStr);
                        JOptionPane.showMessageDialog(this,
                                "ID范围必须为整数",
                                "输入错误",
                                JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                }
                // 无查询条件
                else {
                    System.out.println("【调试】未提供查询条件");
                    JOptionPane.showMessageDialog(this,
                            "请输入至少一个查询条件",
                            "提示",
                            JOptionPane.WARNING_MESSAGE);
                    return;
                }

                // 处理查询结果
                if (searchResult.isEmpty()) {
                    System.out.println("【调试】查询结果为空");
                    JOptionPane.showMessageDialog(this,
                            "未找到匹配的存款业务",
                            "查询结果",
                            JOptionPane.INFORMATION_MESSAGE);
                } else {
                    System.out.println("【调试】查询成功，找到 " + searchResult.size() + " 条记录");
                    dispose();
                }
            } catch (Exception ex) {
                System.err.println("【错误】查询失败: " + ex.getMessage());
                ex.printStackTrace();
                JOptionPane.showMessageDialog(this,
                        "查询失败: " + ex.getMessage(),
                        "错误",
                        JOptionPane.ERROR_MESSAGE);
            }
        });

        cancelButton.addActionListener(e -> {
            System.out.println("【调试】用户取消查询");
            dispose();
        });

        add(panel);
    }

    public List<DepositType> getSearchResult() {
        return searchResult;
    }
}